import { Injectable } from '@angular/core';
// import {ProductUser} from '../class/product-user';
// import {Product} from '../class/product';
import { Observable } from 'rxjs/Observable';
import {ProductNotes} from '../class/product-note';
import { HttpService } from './http.service'
import { ProductDetail } from './../class/product-detail';
import { ProductListPage } from './../class/product-list-page';

@Injectable()
export class ProductService {

  constructor(private http: HttpService) { }

  simpleDeleteProduct(productId: any): any {
    let url = `/products/${productId}`;
    return this.http.delete(url)
       .map(res => res.json())
       .catch(this.handlerError);
  }

  // ModifyCondition( url: string, params: any ): Observable<any> {
  ModifyCondition(params: any ): Observable<any> {
    let body = JSON.stringify(params);
    let url = `/modify/condition/`;
    console.log(`params body===> ${url} - ${body}`);
    return this.http.put(url, body)
      .map(this.extractData)
      .catch(this.handlerError);
  }


  updateCondition(data: any) {
    let url  = `/products/`;
    let body = JSON.stringify(data);
    console.log(`current request url: `, url);
    return this.http.put(url, body)
       .toPromise()
       .then()
       .catch(this.handlerError);
  }

  // public getProductInfo( uuid: string ): Observable<any> {
  public getProductInfo( id: string ): any {
    let url = `/${id}`;
    console.log(`current product user uuid ===> ${url} - ${id}`);
    return this.http.get(url)
      .map(res => res.json())
      .catch(this.handlerError);
  }


  public deleteProduct(productId: any[]) {
    console.log(`current product ID : ` + productId);
    let url = `/products/${productId}`;
    console.log(`current request url : ` + url);
    return this.http.delete(url)
       .map((response: Response) => response.json())
       .catch(this.handlerError);
  }

  public AddNote(data: any[]): Observable<any> {
    let url = `/product_notes/`;
    return this.http.post(url, data)
        .map(this.extractData)
        .catch(this.handlerError);
  }

  public AddProduct(data: object): Observable<any> {
  // public AddProduct(data: any[]): Observable<any> {
    let url = `/products/`;
    // let body = JSON.stringify(data);
    console.log('current request url: ' + url);
    return this.http.post(url, data)
      .map(this.extractData)
      .catch(this.handlerError);
  }

  // return
  public DeleteNote (id: any): Observable<any> {
    console.log(`current content ===> ${id}`);
    let url = `/product_notes/${id}`;
    return this.http.delete(url)
      .map(this.extractData)
      .do(data => console.log('delete note: ' + JSON.stringify(data)))
      .catch(this.handlerError);
  }


  public deletedNote(id: number) {
    let url = `/product_notes/${id}`;
    return this.http.delete(url)
        .toPromise()
        .then(() => null)
        .catch(this.handlerError);
  }


  public pagiField(start: number, end: number) {
    let url = `/products?_start=${start}&_end=${end}`;
    console.log(`current paginate url ===> ${url}`);
    return this.http.get(url)
      .map(res => res.json())
      .catch(this.handlerError);
  }

  public pagiFieldPro(start: number, end: number, stat: any) {
    let url = `/products?_start=${start}&_end=${end}`;
    if (stat != undefined) {
      url = url + `&status=${stat}`;
    }
    console.log(`current paginate url ===> ${url}`);
    return this.http.get(url)
      .map(res => res.json())
      .catch(this.handlerError);
  }


  public generatePaginate(start: number, end: number, stat?: any, date?: any, tags?: any, sort?: any, order?: any) {
    
    let url = `/products?_start=${start}&_end=${end}`;
    if (stat != undefined) {
      url = url + `&_status=${stat}`;
      console.log(url, '<--- status url');
    } else if (date != undefined) {
      url = url + `&_date=${date}`;
      console.log(`date url --->`, url);
    } else if (tags != undefined) {
      url = url + `&_tags${tags}`;
      console.log(`tags url --->`, url);
    } else if (sort != undefined && order != undefined) {
      url = url + `&_sort=${sort}&_order=${order}`;
      console.log(`sort and order url --->`, url);
    }

    return this.http.get(url)
       .map((response: Response) => response.json())
       .catch(this.handlerError);
  }


  public noteListPaginate(start: number, end: number) {
    let url = `/product_notes?_start=${start}&_end=${end}`;
    console.log(`current request url ===> ${url}`);
    return this.http.get(url)
       .map(res => res.json())
       .catch(this.handlerError);
  }

  public noteListPagiPro(start: number, end: number, index: number) {
    let url = `/product_notes?_start=start&_end=end`;
    if (index != undefined) {
      url = url + `&_index=index`;
    }
    console.log(`current request url:` + url);
    return this.http.get(url)
       .map((response: Response) => { response => response.json() })
       .do(data => console.log('note list paginate pro:' + JSON.stringify(data)))
       .catch(this.handlerError);
  }

  public statusEvent(statusValue: string): any {
    let url = `/products?q=${statusValue}`;
    console.log(`current request url ===> ${url}`);
    return this.http.get(url)
       .map((response: Response) => response.json())
       .do(data => console.log('status filter:' + JSON.stringify(data)))
       .catch(this.handlerError);
  }

  public tagEvent(tagsValue: any): any {
    let url = `/products?_tags=${tagsValue}`;
    console.log(`current request url:` + url);
    return this.http.get(url)
      .map((response: Response) => response.json())
      .do(data => console.log('tags filter:' + JSON.stringify(data)))
      .catch(this.handlerError);
  }

  public dateEvent(dateValue: any): any {
    let url = `/products?_date=${dateValue}`;
    console.log(`current request url:` + url);
    return this.http.get(url)
       .map((response: Response) => response.json())
       .do(data => console.log('date filter:' + JSON.stringify(data)))
       .catch(this.handlerError);
  }


  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }

  private handlerError(error: any) {
    let errMsg = (error.message) ? error.message:
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg);
        return Observable.throw(errMsg);
  }

}
